package com.sunshine.lnuplus.ui.activity

import android.content.*
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.base.BaseActivity
import com.sunshine.lnuplus.internet.CheckUpdate
import com.sunshine.lnuplus.utils.BlurBuilder
import com.sunshine.lnuplus.utils.StatusBarUtils
import com.sunshine.lnuplus.view.DialogWithoutView
import es.dmoral.toasty.Toasty
import kotlinx.android.synthetic.main.activity_about.*
import java.lang.Exception


class AboutActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_about)

        StatusBarUtils.setStatusBarWithImage(this)
        initView()
    }

    private fun initView() {
        setNoTitle()
        setSupportActionBar(about_toolbar)
        supportActionBar!!.apply {
            setDisplayShowTitleEnabled(false)
            setDisplayHomeAsUpEnabled(true)
        }
        about_toolbar.apply {
            title = "关于"
            setNavigationOnClickListener { supportFinishAfterTransition() }
        }
        about_background.setImageBitmap(BlurBuilder.blur(
            this,
            BitmapFactory.decodeResource(resources, R.mipmap.background),
            25f
        ))
        imageView_lnu.setOnLongClickListener {
            startActivity(Intent(this, SuperLoginActivity::class.java).putExtra("test", true))
            true
        }
        gitee.setOnClickListener {
            val bundle = Bundle()
            bundle.putString("title","开放源代码")
            bundle.putString("url","https://gitee.com/sunshine0523/lnuplus")
            startActivity(Intent(this, WebViewActivity::class.java).putExtras(bundle))
        }
        tell_me.setOnClickListener {
            try{
                startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(resources.getString(R.string.qq_url))))
            }catch (e: Exception){
                Toasty.warning(this, resources.getString(R.string.not_found_qq)).show()
            }
        }
        tell_me2.setOnClickListener {
            startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("http://www.coolapk.com/u/810697")))
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.about_menu, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            R.id.give_me -> {
                val positive = DialogInterface.OnClickListener { _, _ ->
                    try {
                        Toasty.info(this, "感谢！").show()
                        startActivity(Intent.parseUri(resources.getString(R.string.alipay_url), Intent.URI_INTENT_SCHEME))
                    }catch (e: Exception){
                        Toasty.warning(this, "打开支付宝失败").show()
                    }
                }
                DialogWithoutView.newInstance("感谢你",
                    resources.getString(R.string.thank_you),
                true,
                    positive,
                    null
                ).show(supportFragmentManager, "help_me")
            }
            R.id.check_update -> {
                CheckUpdate(this, supportFragmentManager, true)
            }
        }
        return true
    }
}
